<?php
class EmployersController extends AppController {

	var $name = 'Employers';
	var $helpers = array('Html', 'Form');
    
    function checkname()
    {
        $this->layout = 'ajax';
        //$pass=$this->Jobseeker->query("SELECT count(*) FROM `jobseekers` WHERE username='jobseeker1'");        
        $this->Employer->recursive = -1; 
        $pass=$this->Employer->find('count',array( 'conditions'=> array( 'Employer.username'=>$this->data['Employer']['username'])));
        $this->set('pass',$pass);
    }

    function signup()
    {

        $this->set('choose1',4);
        if( $this->Session->read('User.type')=='employer' )
        { $this->redirect(array('controller'=>'employers', 'action'=>'index'),null, true); }
        
        if(!empty($this->data))
        {
            //$pass = $this->Employer->find('count',array( 'conditions'=> array( 'or' => array('Employer.username'=>$this->data['Employer']['username'], 'Employer.email'=>$this->data['Employer']['email'], 'Employer.cp_email'=>$this->data['Employer']['email'], 'Employer.email'=>$this->data['Employer']['cp_email'], 'Employer.cp_email'=>$this->data['Employer']['cp_email'] ))));
            
            $query = "SELECT count(*) as jum FROM `employers` WHERE username ='".$this->data['Employer']['username']."' OR email='".$this->data['Employer']['email']."' OR email='".$this->data['Employer']['cp_email']."' OR cp_email='".$this->data['Employer']['email']."' OR cp_email='".$this->data['Employer']['cp_email']."'";
            $pass = $this->Employer->query($query);
            if($pass[0][0]['jum']==0)
            {
                    if($this->data['Employer']['slip']['type']=='image/gif' || $this->data['Employer']['slip']['type']=='image/jpeg' || $this->data['Employer']['slip']['type']=='image/pjpeg' || $this->data['Employer']['slip']['type']=='image/png')
                    {
                        
                        $name = substr($this->data['Employer']['slip']['name'],0,strpos($this->data['Employer']['slip']['name'],'.'));
                        $type = substr($this->data['Employer']['slip']['name'],strpos($this->data['Employer']['slip']['name'],'.'),5); 
                        $destination = EMPLOYER_SLIP_DIRECTORY.$this->data['Employer']['company_name'].$type;
                        if( move_uploaded_file($this->data['Employer']['slip']['tmp_name'],$destination)  )
                        {
                            $this->data['Employer']['slip']=$destination;
                            $this->Employer->create();
                            $this->Employer->save($this->data); 
                            $this->Session->setFlash(__('Congratulations, your account has been created. Please wait for 24 hours of bussiness day for admin to validate.', true));
                            $this->set('sukses',1);                        

                        }                                                                        
                        else
                        { $this->Session->setFlash(__('The slip is failed to be uploaded, please try again', true));}
                    }
                    else
                    { $this->Session->setFlash(__('Please provide image file', true)); }  
            }
            else
            {
                $this->Session->setFlash(__('There is an error, your username or either of your emails has been used', true));
            }
        }   
        $cities = $this->Employer->City->find('list',array('order'=>'City.name')); 
        $this->set('cities',$cities); 
    }

    function login()
    {
        
        $this->set('choose1',4);
        if( $this->Session->read('User.type')=='jobseeker' )
        { $this->redirect(array('controller'=>'jobseekers', 'action'=>'index'),null, true); }

        if( $this->Session->read('User.type')=='employer' )
        { $this->redirect(array('controller'=>'employers', 'action'=>'index'),null, true); }
        
        if(!empty($this->data))
        {
            $this->Employer->recursive=-1;
            $username=mysql_real_escape_string($this->data['Employer']['username']);
            $password=mysql_real_escape_string($this->data['Employer']['password']);
            
            $data=$this->Employer->find('first',array('conditions'=>array('Employer.username'=>$username,'Employer.password'=>$password)));
            if( $data )
            {

                if($data['Employer']['status']==1)
                {
                    $temp['type']= 'employer';
                    $temp['id']= $data['Employer']['id'];
                    $temp['username']= $data['Employer']['username'];
                    $temp['password']= $data['Employer']['password'];
                
                    $this->Session->write('User',$temp);
                    $this->redirect(array('controller'=>'employers', 'action'=>'index'),null, true);
                }
                else
                {
                      $this->Session->setFlash(__('There is an error, your account hasn\'t been approved yet, please contact our team for the details', true));                 
                }
            }
            else
            {
                $this->Session->setFlash(__('There is an error, wrong username or password', true));
            }
        }
    }
    
    function logout()
    {
        $this->Session->del('User');
        $this->redirect(array('controller'=>'jobs', 'action'=>'index'));
    }

    function company_profile()
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->set('choose1',46);
            if(!empty($this->data))
            {
                $this->data['Employer']['id']= $this->Session->read('User.id');
                $this->Employer->recursive = -1;
                $data = $this->Employer->find('count',array('conditions'=>"id <> ".$this->data['Employer']['id']." AND ( email = '".$this->data['Employer']['email']."' OR cp_email = '".$this->data['Employer']['email']."' )"));
                if($data==0)
                {
                    $this->data['Employer']['desc'] = strip_tags($this->data['Employer']['desc'],'<br>');
                    $this->data['Employer']['other'] = strip_tags($this->data['Employer']['other'],'<br>');
                    $this->Employer->save($this->data);
                    $this->Session->setFlash(__('Your company profile has been saved', true));
                    $this->redirect(array('controller'=>'employers', 'action'=>'company_profile'),null, true); 
                }
                else
                {
                    $this->Session->setFlash(__('There is an error, the email has been used ', true));
                }

            }
            if (empty($this->data))
            {
                $this->Employer->recursive = -1;
                $this->data = $this->Employer->read(null, $this->Session->read('User.id'));
            }
            $cities = $this->Employer->City->find('list',array('order'=>'City.name')); 
            $this->set('cities',$cities);   
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);
        }        
    }

    function logo()
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->set('choose1',46);
            if(!empty($this->data))
            {
                if($this->data['Employer']['logo']['name']!='')
                {
                    if($this->data['Employer']['logo']['type']=='image/gif' || $this->data['Employer']['logo']['type']=='image/jpeg' || $this->data['Resume']['logo']['type']=='image/pjpeg' || $this->data['Employer']['logo']['type']=='image/png')
                    {
                        $size = getimagesize($this->data['Employer']['logo']['tmp_name']);
                        if($size[0]<=125 && $size[1]<=50)
                        {

                            $this->Employer->recursive = -1;
                            $data = $this->Employer->read(null,$this->Session->read('User.id'));
                            
                            if( file_exists($data['Employer']['logo']) && $data['Employer']['logo']!='' && $data['Employer']['logo']!='n/a' )
                            {
                                unlink($data['Employer']['logo']);
                            }

                            $name = substr($this->data['Employer']['logo']['name'],0,strpos($this->data['Employer']['logo']['name'],'.'));
                            $type = substr($this->data['Employer']['logo']['name'],strpos($this->data['Employer']['logo']['name'],'.'),5); 
                            $destination = LOGO_DIRECTORY.$this->data['Employer']['id'].$type;
                            //var_dump($tipe);
                            
                            if( move_uploaded_file($this->data['Employer']['logo']['tmp_name'],$destination)  )
                            {

 
                                $this->Session->setFlash(__('Your logo has been uploaded.', true));
                                $this->data['Employer']['logo']=$destination;
                                $this->Employer->save($this->data); 
                            
                            }
                            else
                            { $this->Session->setFlash(__('The file is failed to be uploaded, please try again.', true));}
                        }
                        else
                        { $this->Session->setFlash(__('Maximum size is 125 x 50 pixel', true)); }
                    }
                    else
                    { $this->Session->setFlash(__('Please provide image file (.jpeg, .jpg, .gif, .png)', true)); }                    
                }
                else
                { $this->Session->setFlash(__('Please provide image file (.jpeg, .jpg, .gif, .png)', true)); }                    
            }
            
            $this->Employer->recursive = -1;
            $this->data = $this->Employer->read(null, $this->Session->read('User.id'));
          
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);
        }        
    }

    function delete_logo()
    {
        if($this->Session->read('User.type')=='employer')
        {
                $this->Employer->recursive = -1;
                $this->data = $this->Employer->read(null,$this->Session->read('User.id'));
                
                if( file_exists($this->data['Employer']['logo']) && $this->data['Employer']['logo']!='' && $this->data['Employer']['logo']!='n/a' )
                {
                    unlink($this->data['Employer']['logo']);
                    $this->Session->setFlash(__('Your logo has been delete.', true));
                }
                else
                {
                    $this->Session->setFlash(__('You don\'t have uploaded logo', true));
                }
                
                $this->data['Employer']['logo']='';
                $this->Employer->save($this->data); 
                $this->redirect(array('controller'=>'employers','action'=>'logo'));           
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true); 
        }  
    }    
    
    function edit_account()
    {
        if($this->Session->read('User.type')=='employer')
        {
             $this->set('choose1',47);
            if(!empty($this->data))
            {
                
                $this->data['Employer']['id']= $this->Session->read('User.id');
                $this->Employer->recursive = -1;
                $data = $this->Employer->find('count',array('conditions'=>"id <> ".$this->data['Employer']['id']." AND ( email = '".$this->data['Employer']['cp_email']."' OR cp_email = '".$this->data['Employer']['cp_email']."' )"));

                if($data==0)
                {
                    $this->Employer->save($this->data);
                    $this->Session->setFlash(__('Your account has been saved', true));
                }
                else
                {
                    $this->Session->setFlash(__('There is an error, this email has been used ', true));
                }
            }
                $this->Employer->recursive = -1;
                $this->data = $this->Employer->read(null, $this->Session->read('User.id'));
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);        
        }

    }

    function change_password()
    {
        if($this->Session->read('User.type')=='employer')   
        {
            $this->set('choose1',47);
            if(!empty($this->data))
            {
                $this->Employer->recursive = -1;
                $data = $this->Employer->read(null, $this->Session->read('User.id'));
            
                if($data['Employer']['password']==$this->data['Employer']['old_password'])
                { 
                    $this->data['Employer']['id']= $this->Session->read('User.id'); 
                    $this->Employer->save($this->data);
                    $this->Session->setFlash(__('Your password has been change', true));
                    $this->redirect(array('controller'=>'employers', 'action'=>'edit_account'),null, true); 
                }
                else
                { $this->Session->setFlash(__('Your old password is wrong', true)); }
            }
            
        }
        else
        {
            $this->set('choose1',4);
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);        
        }
    }    
    
	function index() 
    {
        if($this->Session->read('User.type')=='employer')   
        {
            $this->set('choose1',41);
            //echo $query."<br>";
            
            $query = "SELECT * FROM `jobs` WHERE status=1 AND end_date < '".date("Y-m-d")."' AND employer_id = ".$this->Session->read('User.id'); 
            $expired_job = $this->Employer->query($query);
            $this->set('expired_job',$expired_job);
            
            $query = "SELECT * FROM `jobs` WHERE status=1 AND end_date >= '".date("Y-m-d")."' AND employer_id = ".$this->Session->read('User.id'); 
            $posted_job = $this->Employer->query($query);
            $this->set('posted_job',$posted_job);
            
            $query = "SELECT * FROM `jobs` WHERE status=0 AND employer_id = ".$this->Session->read('User.id'); 
            $waited_job = $this->Employer->query($query);
            $this->set('waited_job',$waited_job);
            
            
            
            $query = "SELECT * FROM `advs` WHERE status=1 AND end_date < '".date("Y-m-d")."' AND employer_id = ".$this->Session->read('User.id'); 
            $expired_adv = $this->Employer->query($query);
            $this->set('expired_adv',$expired_adv);

            $query = "SELECT * FROM `advs` WHERE status=1 AND end_date >= '".date("Y-m-d")."' AND employer_id = ".$this->Session->read('User.id'); 
            $posted_adv = $this->Employer->query($query);
            $this->set('posted_adv',$posted_adv);
            
            $query = "SELECT * FROM `advs` WHERE status=0 AND employer_id = ".$this->Session->read('User.id'); 
            $waited_adv = $this->Employer->query($query);
            $this->set('waited_adv',$waited_adv);
        }
        else
        {
            $this->set('choose1',4);
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);
        }        
	}

	function view($id = null) 
    {
        $this->layout = 'company';
        $this->Employer->recursive=-1;
        $data = $this->Employer->find('first',array('conditions'=>array('Employer.status'=>1,'id'=>$id)));
        if(!empty($data))
        {
            $jobs = $this->Employer->Job->find('all',array('conditions'=>array('Job.employer_id'=>$id,'Job.status'=>1),'order'=>'Job.created'));
            $this->set('data',$data);
            $this->set('jobs',$jobs);
        }
        else
        { 
            $this->Session->setFlash(__('Invalid ID for company profile', true)); 
            $this->redirect(array('controller'=>'jobs', 'action'=>'index'),null, true);        
        }
	}
    
    
    function index_admin()  
    {
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout='ajax';
        }
        else
        {
            $this->Session->setFlash('You are forbidden for this page');
            $this->redirect(array('controller'=>'jobs', 'action'=>'login_admin'),null, true);
        }             
    }
 
    function filter_admin()  
    {
        $this->layout='ajax';
        if(!empty($this->data['category']))
        {
            $query = "SELECT employers.*,cities.name FROM `employers`,`cities` WHERE employers.city_id = cities.id ";
            if($this->data['category']==2)
            { $query.=" AND employers.status = 0"; }
            $mydate = date("Y-m-d", mktime(0, 0, 0, date('m')-6, date('d'), date('Y')));
            if($this->data['category']==3)
            { $query.=" AND employers.status = 0 AND employers.created < '".$mydate."'"; }
            
            
            if($this->data['id']!='')
            { $query.= " AND employers.id=".$this->data['id']; }
            if($this->data['name']!='')
            { $query.= " AND employers.company_name LIKE '%".$this->data['name']."%' ";}
            
            $employers = $this->Employer->query($query);
            $this->set('employers',$employers);
        }
        
        if(empty($this->data))
        {
            $query = "SELECT employers.*,cities.name FROM `employers`,`cities` WHERE employers.city_id = cities.id ";
            $employers = $this->Employer->query($query);
            $this->set('employers',$employers);
        }
    } 
    
    function edit_admin( $id = null )
    {
        if(!empty($this->data))
        {
            $this->Employer->save($this->data);
            $this->redirect(array('controller'=>'employers','action'=>'filter_admin'));    
        }
        else
        {
            $this->layout = 'ajax';
            $this->Employer->recursive=-1;
            $this->data = $this->Employer->read(null,$id);
			// var_dump($this->data);
        }
    }

    function delete_admin($id)
    {  
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            $this->Employer->del($id);
            $this->redirect(array('controller'=>'employers','action'=>'filter_admin'));
        }        
    }
    
    function slip_admin( $id = null )
    { 
        $this->layout = 'ajax';
        $this->Employer->recursive=-1;        
        $data = $this->Employer->read(null,$id);
        $this->set('data',$data);   
    
    }    
    
    function approve_admin( $id = null )
    { 
        $mydate = date("Y-m-d", mktime(0, 0, 0, date('m')+1, date('d'), date('Y')));
        $this->data['Employer']['id']=$id;
        $this->data['Employer']['status']=1;
        $this->Employer->save($this->data);
        $this->redirect(array('controller'=>'employers','action'=>'filter_admin'));
    }    

}
?>